<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
    private $_id;
    private $_role;
    const ERROR_NOT_ACTIVATED = 3;

	/**
	 * Authenticates a user.
	 * The example implementation makes sure if the username and password
	 * are both 'demo'.
	 * In practical applications, this should be changed to authenticate
	 * against some persistent user identity storage (e.g. database).
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{

        $record = Users::model()->findByAttributes(array('name'=>$this->username));
        if($record === null)
        {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        }
        elseif($record->password !== $this->hashPass($this->password))
        {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        }
        elseif($record->active == 0)
        {
            $this->errorCode = self::ERROR_NOT_ACTIVATED;
        }
        else
        {
            $this->_id = $record->id;
            $this->_role = $record->role;
            $this->setState('role', $record->role);
            $this->errorCode = self::ERROR_NONE;
        }
        return !$this->errorCode;
	}

    public function getId()
    {
        return $this->_id;
    }
    public function getRole()
    {
        return $this->_role;
    }
    public function hashPass($pass)
    {
        return md5($pass);
    }


}